package edu.java.day0724;

import java.io.*;
import java.net.URL;

/*
转换流：InputStreamReader OutputStreamWriter
1.以字符流的形式操作字节流
2.指定字符集
 */
class ConvetTest02 {
    public static void main(String[] args) {
        //操作网络流  下载百度的源代码
        try(BufferedReader reader=
                    new BufferedReader(
                            new InputStreamReader(
                                    new URL("http://www.baidu.com").openStream(),"UTF-8"));
            BufferedWriter writer=
                new BufferedWriter(
                        new OutputStreamWriter(
                                new FileOutputStream("D:/study/后端学习/freeprogramming/fpa/liu_jun_jun/src/baidu.html"),"UTF-8"));){
            //3、操作（读取）
            String msg;
            while((msg=reader.readLine())!=null){
                //System.out.print(msg);
                writer.write(msg);//字符集不统一
                writer.newLine();
            }
        }catch (IOException e){
            System.out.println("操作异常");
        }



    }
    public static void test2() {
        //操作网络流  下载百度的源代码
        try(InputStreamReader is=new InputStreamReader(new URL("http://www.baidu.com").openStream(),"UTF-8");){
            //3、操作（读取）
            int temp;
            while((temp=is.read())!=-1){
                System.out.print((char) temp);
            }

        }catch (IOException e){
            System.out.println("操作异常");
        }

    }
    public static void test1() {
        //操作网络流  下载百度的源代码
        try(InputStream is=new URL("http://www.baidu.com").openStream();){
            //3、操作（读取）
            int temp;
            while((temp=is.read())!=-1){
                System.out.print((char) temp);//字节数不够出现乱码
            }

        }catch (IOException e){
            System.out.println("操作异常");
        }



    }
}
